package com.alibaba.sdk.abstracts;

import org.junit.platform.commons.util.StringUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 测试类通用工具
 *
 * @ writer Shawn(Shawn_0217@163.com)
 * @ date & time 2021/4/28 20:41
 * @ company alibaba
 */
public abstract class UtilsAbstract {

    /**
     * 生成文件上传 MultipartFile
     * @param fileUpload
     * @return
     * @throws IOException
     */
    public MultipartFile initMultipartFile(String fileUpload) throws IOException {
        if (StringUtils.isBlank(fileUpload)) {
            throw new RuntimeException("输入上传路径为空");
        }
        File file = new File(fileUpload);
        if (!file.exists()) {
            throw new RuntimeException("不存在文件:" + file.getAbsolutePath());
        }
        InputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), "multipart/form-data", inputStream);
        return multipartFile;
    }
}
